﻿using System;
using System.Linq;
using System.Web.Mvc;
using MvcContrib.UI.Grid;
using MvcContrib.Sorting;
using QDT.Common;
using QDT.Core.Experience;
using QDT.MVC.Security;
using QDT.Common.Data;
using QDT.Services.Experience;

namespace QDT.Web.Admin.Controllers
{
    [SecurityModule(Name = "体验宝管理")]
    public class ExperienceController : BaseController
    {

        [SecurityNode(Name = "体验宝列表")]
        public ActionResult Index(int? page, GridSortOptions sort, int? projectId, string name, DateTime? start, DateTime? end, int? status, int? days)
        {

            var filter = PredicateExtensionses.True<ExpProject>();

            if (days != null && days != 0) filter = filter.And(x => x.Days == days);

            if (projectId != null && projectId != 0) filter = filter.And(x => x.ID == projectId);

            if (!string.IsNullOrEmpty(name)) filter = filter.And(x => x.Name.Contains(name));

            if (status != null) filter = filter.And(x => x.Status == status);

            if (start.HasValue) filter = filter.And(x => x.CreateTime >= start.Value);

            if (end.HasValue) filter = filter.And(x => x.CreateTime <= end.Value);

            const int pageSize = 20;

            var pageNumber = page ?? 1;

            if (sort.Column == null) sort.Column = "ID";

            ViewData["sort"] = sort;

            var deals = DbProvider.D<ExpProject>().Where(filter).OrderBy(sort.Column, sort.Direction == SortDirection.Descending);

            return View(deals.ToPagedList(pageNumber - 1, pageSize));

        }
        [SecurityNode(Name = "编辑体验宝页")]
        public ActionResult Edit(int id)
        {
            var peoject = DbProvider.GetByID<ExpProject>(id);
            return View(peoject);
        }
        [SecurityNode(Name = "编辑体验宝操作")]
        public ActionResult PostEdit(int id, string name, decimal minLoanMoney, decimal maxLoanMoney)
        {
            var peoject = DbProvider.GetByID<ExpProject>(id);
            peoject.Name = name;
            peoject.MinLoanMoney = minLoanMoney;
            peoject.MaxLoanMoney = maxLoanMoney;
            DbProvider.SaveChanges();
            return Success("操作成功");
        }
        [SecurityNode(Name = "添加体验宝页")]
        public ActionResult Add()
        {
            return View();
        }
        [SecurityNode(Name = "添加体验宝操作")]
        public ActionResult PostAdd(string name, decimal borrowAmount, double rate, int days, decimal minLoanMoney, decimal maxLoanMoney)
        {

            var peoject = new ExpProject
            {
                Name = name,
                Rate = rate,
                Days = days,
                BorrowAmount = borrowAmount,
                MinLoanMoney = minLoanMoney,
                MaxLoanMoney = maxLoanMoney == 0 ? borrowAmount : maxLoanMoney
            };

            DbProvider.Add(peoject);
            DbProvider.SaveChanges();

            return Success("操作成功");
        }

        public PartialViewResult ShowDetail(int id)
        {
            var credits = DbProvider.D<ExpCredit>().Where(x => x.ExpProjectId == id).ToList();
            return PartialView(credits);
        }
        [SecurityNode(Name = "更改体验宝状态页")]
        public ActionResult ChangeStatus(int id)
        {
            var project = DbProvider.GetByID<ExpProject>(id);
            return PartialView(project);
        }
        [SecurityNode(Name = "更改体验宝状态操作")]
        public ActionResult PostChange(int id, DateTime? startTime)
        {
            if (!startTime.HasValue || startTime.Value < DateTime.Now.AddMinutes(-10)) return Error("时间日期输入错误！");

            var project = DbProvider.GetByID<ExpProject>(id);
            if (project == null) throw new NullReferenceException("Project不存在或者已经被删除");

            if (startTime < project.CreateTime) throw new NullReferenceException("日期输入错误");

            project.Status = 1;
            project.StartTime = startTime;

            DbProvider.SaveChanges();

            return Success("操作成功");
        }

        public ActionResult Refund(int id)
        {
            try
            {
                IoC.Resolve<IExpService>().Refund(id);
            }
            catch (Exception e)
            {
                return Json(new { Status = "false", Message = "操作失败 : " + e.Message });
            }
            return Json(new { Status = "true", Message = "操作成功" });
        }

    }
}
